/**  
 *	VisualGroup JavaScript prototype Classes, version 0.1
 *  (c) 2008 Bill Vison <vison_ruan@126.com>
 *
 *
 /*--------------------------------------------------------------------------*/
using("Vsg.Component");

Vsg.Component.TriggerPanel = Ext.extend(Ext.form.FormPanel, {

    defaultMethod: function($config){
        // --- defind basic defined ---
        
        // ---- set the combox data 
        var store = new Ext.data.ArrayStore({
            fields: ['code', 'name'],
            data: [["1", '简单定时触发器'], ["2", "Cron定时触发器"]]
        });
        
        var $_con = {
            id: "Vsg.Component.TriggerPanel",
            layout: 'form',
            bodyStyle: 'padding:5px 5px 0',
            items: [{
				xtype:'fieldset',
	        	labelWidth: 100,
				title:'调度任务信息',
				autoHeight:true,
				defaults: {width: 250},
				defaultType: 'textfield',
				items:[{
					fieldLabel: '任务编码',
					name:'code',
					xtype:'textfield',
					readOnly :'true',
					allowBlank:false
				},{
					fieldLabel: '调度任务名称',
					name:'name',
					xtype:'textfield',
					readOnly :'true',
					allowBlank:false
				},{
					fieldLabel: '调度任务类',
					xtype:'textfield',
					readOnly :'true',
					name:'jobClass',
					allowBlank:false
				}]
        	},{
				xtype:'fieldset',
				title:'定时触发器',
				autoHeight:true,
				labelWidth: 80,
				defaults: {width: 210},
				defaultType: 'textfield',
				items:[{
                	xtype: "combo",
                	fieldLabel: "触发器类型",
                	hiddenName: "triggerType",
                	hiddenId: "triggerType",
					name:'trigType',
                	store: store,
                	displayField: "name",
                	valueField: "code",
                	typeAhead: true,
                	editable: false,
                	mode: "local",
                	triggerAction: "all",
                	emptyText: "请选择定时触发器...",
                	selectOnFocus: true,
                	listeners: {
                    	scope: this,
                    	'select': this.selectItem
                	}
            	}]
			}]
        };
        
        
        try {
        
            $_con.id = $config.id != null ? $config.id : "Vsg.Component.TriggerPanel";
            
            for (var $i in $config) {
                $_con[$i] = $config[$i];
            }
            
        } 
        catch (e) {
            // -- catch default value defined ---
            alert(e);
        }
        return $_con;
        
    },
    
    /**
     * constructor method
     * @param {Object} $config
     */
    constructor: function($config){
        var $attrs = this.defaultMethod($config);
		
        Ext.applyIf(this, $attrs);
        
		Vsg.Component.TriggerPanel.superclass.constructor.call(this, $attrs);
        
        
    },

  	changeToSimpleTrigger: function(){
        var fieldset = {
            id: 'simpleTriggerDate',
            xtype: 'fieldset',
            title: '起始日期设定',
            autoHeight: true,
            items: [{
                layout: 'column',
                border: false,
                items: [{
                    columnWidth: 0.6,
                    layout: 'form',
                    labelWidth: 60,
                    defaults: {
                        width: 100
                    },
                    border: false,
                    items: [{
                        fieldLabel: '开始日期',
                        name: 'startDate',
                        format: 'Y-m-d',
                        xtype: 'datefield',
                        emptyText: '必填项',
                        allowBlank: false
                    }, {
                        fieldLabel: '结束日期',
                        name: 'endDate',
                        format: 'Y-m-d',
                        xtype: 'datefield'
                    }]
                }, {
                    columnWidth: 0.4,
                    layout: 'form',
                    labelWidth: 40,
                    defaults: {
                        width: 80
                    },
                    border: false,
                    items: [{
                        fieldLabel: '时间',
                        name: 'startTime',
                        xtype: 'timefield',
                        minValue: '00:00',
                        maxValue: '23:59',
                        format: 'H:i',
                        increment: 15,
                        emptyText: '必填项',
                        allowBlank: false
                    }, {
                        fieldLabel: '时间',
                        name: 'endTime',
                        minValue: '00:00',
                        maxValue: '23:59',
                        format: 'H:i',
                        increment: 15,
                        xtype: 'timefield'
                    }]
                }]
            }]
        };
        
        var freqFieldset = {
            id: 'freqSetting',
            xtype: 'fieldset',
            title: '频率设定',
            autoHeight: true,
            labelWidth: 60,
            defaults: {
                width: 100
            },
            items: [{
                fieldLabel: '重复次数',
                name: 'repeatCount',
                xtype: 'numberfield'
            }, {
                fieldLabel: '间隔(秒)',
                name: 'repeatInterval',
                xtype: 'numberfield'
            }]
        };
        
        this.remove('cronTrigger', true);
        this.add(fieldset);
        this.add(freqFieldset);
        this.doLayout();
    },

    
    changeToCronTrigger: function(){
        var fieldset = {
            id: 'cronTrigger',
            xtype: 'fieldset',
            title: 'Cron模式设定',
            autoHeight: true,
            labelWidth: 80,
            defaults: {
                width: 210
            },
            defaultType: 'textfield',
            items: [{
                fieldLabel: 'Cron定义',
                name: 'cronPattern',
                xtype: 'textfield',
                allowBlank: false
            }]
        }
        
        this.remove('freqSetting', true);
        this.remove('simpleTriggerDate', true);
        this.add(fieldset);
        this.doLayout();
    },
    
    currentCode: -1, 
    selectItem: function($this, record, index){
        var code = record.get('code');
        
        // --- exist for second times
        if (this.currentCode == code) {
            return;
        }
        this.currentCode = code;
        
        // uset simaple trigger field 
        if (code == 1) {
            this.changeToSimpleTrigger();
        }
        
        else if (code == 2) {
            this.changeToCronTrigger();
        }
    }
    
    
});
